﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.ServiceModel;
using Obra.Api;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using Obra.SmugMug.Api;

namespace Obra.Test
{
    /// <summary>
    /// The purpose of this test (there is only one) is more to validate the 
    /// runtime behavior of the contracts.  For example, WCF will barf if the 
    /// UriTemplates are not correct, or ambigous.
    /// </summary>
    [TestFixture]
    public class ISmugMugWcfTester
    {
        //public static Uri MockSmugMugUri = new Uri("http://ipv4.fiddler:9877/smugmug");
        public static Uri MockSmugMugUri = new Uri(Messages.TestUrl);

        private WebServiceHost webServiceHost;

        [SetUp]
        public void SetUp()
        {
            this.webServiceHost = new WebServiceHost(
               typeof(SmugMugServer),
               ISmugMugWcfTester.MockSmugMugUri);

            ServiceEndpoint endpoint = webServiceHost.AddServiceEndpoint(
                typeof(ISmugMug),
                new WebHttpBinding(),
                ISmugMugWcfTester.MockSmugMugUri);

            endpoint.Behaviors.Add(new WebHttpBehavior());

            this.webServiceHost.Open();
        }

        [TearDown]
        public void TearDown()
        {
            if (webServiceHost.State != CommunicationState.Closed)
            {
                webServiceHost.Close();
            }
        }

        [Test]
        public void TestLogonWithPassword()
        {
            using (WebChannelFactory<ISmugMug> factory = new WebChannelFactory<ISmugMug>(
                new WebHttpBinding(),
                ISmugMugWcfTester.MockSmugMugUri))
            {
                ISmugMug client = factory.CreateChannel();
                LoginWithPasswordContract password = client.LoginWithPassword("abc@def.com", "secret", "API");
                Assert.AreEqual("--nick-name--", password.User.NickName);
                Assert.AreEqual("--id--", password.Session.Id);
                Assert.AreEqual(AccountType.Standard, password.AccountType);
            }
        }
    }
}
